<?php


namespace App\Traits;


trait Upload {
	public function uploadImage() {
		$request = request();
		if ( ! $request->hasFile( 'file' ) ) {
			$result = [
				'code' => 400,
				'msg'  => "上传失败!",
				'data' => null,
			];

			return response()->json( $result );
		}
		$path        = $request->file( 'file' )->store( 'upload/invoice', 'public' );
		if ( $path ) {
			$result = [
				'code' => 0,
				'msg'  => "success",
				'data' => [ 'url' => '/storage/' . $path ],
			];

			return response()->json( $result );
		} else {
			$result = [
				'code' => 400,
				'msg'  => "保存失败!",
				'data' => null,
			];

			return response()->json( $result );
		}

	}

	public function uploadInvoice() {
		$request = request();
		if ( ! $request->hasFile( 'file' ) ) {
			$result = [
				'code' => 400,
				'msg'  => "上传失败!",
				'data' => null,
			];

			return response()->json( $result );
		}
		$path        = $request->file( 'file' )->store( 'upload/invoice', 'public' );
		if ( $path ) {
			$result = [
				'code' => 0,
				'msg'  => "success",
				'data' => [ 'url' => '/storage/' . $path ],
			];

			return response()->json( $result );
		} else {
			$result = [
				'code' => 400,
				'msg'  => "保存失败!",
				'data' => null,
			];

			return response()->json( $result );
		}

	}


}